package com.ugoodtech.mdcc.core.utils;

/* Copyright (C) Ugoodtech, Inc - All Rights Reserved.
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 * Proprietary and confidential.
 * Written by Stone Shaw.
 */

public class ByteArrayUtil {
    final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();

    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }

        return new String(hexChars);
    }

    private static byte toByte(char c) {
        byte b = (byte) "0123456789ABCDEF".indexOf(c);
        return b;
    }

    public static byte[] hexStringToByte(String hex) {
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
        }
        return result;
    }

    public static int u16bytesToInt(byte[] src) {
        int value;
        value = ((src[0] & 0xFF)
                | ((src[1] & 0xFF) << 8)
        );
        return value;
    }

    public static Integer getTimeZoneByInteger(Integer value) {
        if (null == value) {
            return null;
        } else {
            Integer result = null;
            switch (value) {
                case 0:
                    result = 1400;
                    break;
                case 1:
                    result = 1345;
                    break;
                case 2:
                    result = 1300;
                    break;
                case 3:
                    result = 1245;
                    break;
                case 4:
                    result = 1200;
                    break;
                case 5:
                    result = 1130;
                    break;
                case 6:
                    result = 1100;
                    break;
                case 7:
                    result = 1030;
                    break;
                case 8:
                    result = 1000;
                    break;
                case 9:
                    result = 930;
                    break;
                case 10:
                    result = 900;
                    break;
                case 11:
                    result = 800;
                    break;
                case 12:
                    result = 700;
                    break;
                case 13:
                    result = 630;
                    break;
                case 14:
                    result = 600;
                    break;
                case 15:
                    result = 545;
                    break;
                case 16:
                    result = 530;
                    break;
                case 17:
                    result = 500;
                    break;
                case 18:
                    result = 430;
                    break;
                case 19:
                    result = 400;
                    break;
                case 20:
                    result = 330;
                    break;
                case 21:
                    result = 300;
                    break;
                case 22:
                    result = 200;
                    break;
                case 23:
                    result = 100;
                    break;
                case 24:
                    result = 0;
                    break;
                case 25:
                    result = -100;
                    break;
                case 26:
                    result = -200;
                    break;
                case 27:
                    result = -300;
                    break;
                case 28:
                    result = -330;
                    break;
                case 29:
                    result = -400;
                    break;
                case 30:
                    result = -430;
                    break;
                case 31:
                    result = -500;
                    break;
                case 32:
                    result = -600;
                    break;
                case 33:
                    result = -700;
                    break;
                case 34:
                    result = -800;
                    break;
                case 35:
                    result = -900;
                    break;
                case 36:
                    result = -930;
                    break;
                case 37:
                    result = -1000;
                    break;
                case 38:
                    result = -1100;
                    break;
                case 39:
                    result = -1200;
                    break;
            }
            return result;
        }
    }

}
